﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Windows.Markup;
using System.Windows.Xps.Packaging;
using DocManagerControls.Controls;
using DocManagerControls.CLASSI;
using System.Windows.Threading; 

namespace DocManager
{
    /// <summary>
    /// Logica di interazione per MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window,IProvider 
    {
        string m_Azione = "";
        Lazy<DocumentiControl> m_DocControl = new Lazy<DocumentiControl>(() => { return new DocumentiControl(); });
        Lazy<CodificheControl> m_CodificheControl = new Lazy<CodificheControl>(() => { return new CodificheControl(); });
        //Lazy<RicercaDocControl> m_RicercaDocControl =  new Lazy<RicercaDocControl>(() => { return new RicercaDocControl(); });
        Lazy<CreaWFControl> m_CreaWFControl = new Lazy<CreaWFControl>(() => { return new CreaWFControl(); });
        Lazy<UtentiControl> m_UtentiControl = new Lazy<UtentiControl>(() => { return new UtentiControl();});
        Lazy<ProcessiControl> m_ProcessiControl = new Lazy<ProcessiControl>(() => { return new ProcessiControl(); });
        Lazy<FEEDReaderControl> m_FeedRssControl = new Lazy<FEEDReaderControl>(() => { return new FEEDReaderControl(); });
        Lazy<NetworkInfoControl> m_NetworkInfoControl = new Lazy<NetworkInfoControl>(()=> { return new NetworkInfoControl(); });
        Lazy<MyDesktopControl> m_MyDesktopControl = new Lazy<MyDesktopControl>(() => { return new MyDesktopControl(); }); 


        public Frame FrameContainer { get {return this.frameContainer ;}}



        public MainWindow()
        {
            InitializeComponent();
                      
            //status bar
            PcUser.Content = Environment.UserName;
            Dominio.Content = Environment.UserDomainName;
            DbName.Content = "";// DocManagerORM.ORM.DocManagerDbEntities;
            DbState.Content = "DB state : " + ( DocManagerORM.ORM.QueryBase.tryOpenConn(true,true) ? "Ok":"Chiuso");
                        
            makeStandardToolBar();
                          
        }


        private void addButtonToStandardToolbar(string pCaption, string pIMGSource)
        {
            addButtonToStandardToolbar(new MenuData() { Caption = pCaption, IMGSource = pIMGSource });        
        }//fm

        private void addButtonToStandardToolbar(MenuData pMenuData)
        {
            var contentTemplate = this.Resources["ButtonMenukTemplate"] as DataTemplate;
            var template = this.Resources["ButtonControlTemplate"] as ControlTemplate;

            var btn = new Button()
            {
                VerticalContentAlignment = System.Windows.VerticalAlignment.Top,
                Background = Brushes.Yellow,
                ContentTemplate = contentTemplate,
                Template = template,
                Content = pMenuData
            };

            btn.Click += new RoutedEventHandler(toolBar1_Click);
            toolBar1.Items.Add(btn);
        }

        private void makeStandardToolBar()
        {
            IList<MenuData> menuItems = new List<MenuData>(4); 
            menuItems.Add( new MenuData() { Caption = "Exit", IMGSource = @"IMG\Exit_RoP.jpg" });
            menuItems.Add(new MenuData() { Caption = "Home", IMGSource = @"IMG\HomeBlu.png" });
            menuItems.Add( new MenuData() { Caption = "Documenti", IMGSource = @"IMG\raccoglitori.png" });
            menuItems.Add( new MenuData() { Caption = "Flussi", IMGSource = @"IMG\iterPicc.gif" });            
            menuItems.Add( new MenuData() { Caption = "News", IMGSource = @"IMG\newsRSS.gif" });
            menuItems.Add(new MenuData() { Caption = "Utenti", IMGSource = @"IMG\Utenti.gif" });
            menuItems.Add(new MenuData() { Caption = "Tabelle", IMGSource = @"IMG\Tables.gif" });
            
            
            foreach (var item in menuItems)
                addButtonToStandardToolbar(item);            
            
        }//fM

        private void menu1_Click(object sender, RoutedEventArgs e)
        {
            MenuHandler(e);
        }//fm

        private void MenuHandler(RoutedEventArgs e)
        {           
            m_Azione = ((MenuItem)e.OriginalSource).Header.ToString().ToLower();
            processaAzioneMenu(m_Azione); 
        }

        private void processaAzioneMenu(string pAzione)
        {
            this.FrameContainer.Content = null;

            switch (pAzione.ToLower())
            {
                case "exit":
                    this.Close();
                    Application.Current.Shutdown();
                    break;
                case "home":
                    m_MyDesktopControl.Value.Visibility = System.Windows.Visibility.Visible;
                    frameContainer.Navigate(m_MyDesktopControl.Value);
                    break;
                case "documenti":
                     m_DocControl.Value.Visibility = System.Windows.Visibility.Visible;
                     frameContainer.Navigate(m_DocControl.Value);
                    break;
                case "ricerche":
                    //m_RicercaDocControl.Value.Visibility = System.Windows.Visibility.Visible;
                    //frameContainer.Navigate(m_RicercaDocControl.Value);
                   break;
                case "tabelle":
                   m_CodificheControl.Value.Visibility = System.Windows.Visibility.Visible;
                   FrameContainer.Content = m_CodificheControl.Value;
                    break;
                case "flussi":
                    m_CreaWFControl.Value.Visibility = System.Windows.Visibility.Visible;
                    frameContainer.Navigate(m_CreaWFControl.Value);
                    break;
                case "utenti":
                    frameContainer.Content = m_UtentiControl.Value;
                    break;
                case "servizi pc":
                    frameContainer.Content = m_ProcessiControl.Value;
                    break;
                case "news":
                    m_FeedRssControl.Value.Visibility = System.Windows.Visibility.Visible;
                    frameContainer.Navigate(m_FeedRssControl.Value);   
                    break;
                case "logview":
                    showLogViewer();
                    break;
                case "networkinfocontrol":
                     m_NetworkInfoControl.Value.Visibility = System.Windows.Visibility.Visible;
                    frameContainer.Navigate(m_NetworkInfoControl.Value); 
                      break;
                default:
                    break;
            }

            frameContainer.BorderBrush = Brushes.DarkCyan;
           
        }//fM

        private void showLogViewer()
        {
            //var viewerPath = System.IO.Path.Combine(Environment.CurrentDirectory, "ExeVari\\TracerX-Viewer.exe");

            ////var pathLog = System.IO.Path.Combine(TracerX.Logger.BinaryFileLogging.Directory,
            ////    TracerX.Logger.BinaryFileLogging.Name);

            //var fullpath =  TracerX.Logger.BinaryFileLogging.FullPath;

            //System.Diagnostics.Process.Start(viewerPath, "\""+ fullpath +"\""); // , Environment.CurrentDirectory 
             

        }//fM



        private void callRicercheFunc()
        {
            // Instantiate and navigate to task page function
            Pages.RicercheFunc taskPageFunction = new Pages.RicercheFunc();
            taskPageFunction.Return += taskPageFunction_Return;
            frameContainer.Content = taskPageFunction;
            //this.NavigationService.Navigate(taskPageFunction);
        
        }//fM

        void taskPageFunction_Return(object sender, ReturnEventArgs<string> e)
        {
            if (e == null) return;
        }

        //Barra grafica
        private void toolBar1_Click(object sender, RoutedEventArgs e)
        {
            var data  = ((Button)e.OriginalSource).Content as MenuData;
            if (data == null) return;

            m_Azione = data.Caption.ToLower();
            processaAzioneMenu(m_Azione);

        }//fM


        void IProvider.AddMenuToPlugingMenu(MenuItem pMenuItem)
        {
            addButtonToStandardToolbar(new MenuData() {Caption= pMenuItem.Header.ToString(), IMGSource = pMenuItem.Icon.ToString()   });   
        }

        private void frameContainer_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            RicalcolaPosizioneWindow();
            //SystemParameters.FullPrimaryScreenHeight 
        }//fM

        private void RicalcolaPosizioneWindow()
        {
            try
            {
                this.Top = (SystemParameters.FullPrimaryScreenHeight / 2) - (this.RenderSize.Height / 2);
                this.Left = (SystemParameters.FullPrimaryScreenWidth / 2) - (this.RenderSize.Width / 2);
            }
            catch { }
        }




    }//fc
}
